package com.atguigu.serviceedu.service.impl;

import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.feign.AliyunVodFeignClient;
import com.atguigu.serviceedu.mapper.EduVideoMapper;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-08-05
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private EduVideoMapper eduVideoMapper;
    @Autowired
    private AliyunVodFeignClient aliyunVodFeignClient;
    @Override
    public boolean removeVideoById(String videoId) {
        aliyunVodFeignClient.deleteVideoById(videoId);
        int i = eduVideoMapper.deleteById(videoId);
        if (i>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void removeOnAliyunVideoByChapterId(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        List<EduVideo> videoList = eduVideoMapper.selectList(wrapper);
        //List<String> collect = videoList.stream().map(v -> v.getId()).collect(Collectors.toList());
        List<String> videoidList=new ArrayList<>();
        for (int i=0;i<videoList.size();i++){
            EduVideo eduVideo = videoList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            videoidList.add(videoSourceId);
        }
        aliyunVodFeignClient.deleteVideoByList(videoidList);
    }
}
